package com.liangzhong.proxy.youngTrain
{
	import com.adobe.serialization.json.JSON;
	import com.liangzhong.commandconst.logic.CMD_Logic;
	import com.liangzhong.commandconst.youngTrain.CMD_YoungTrain;
	import com.liangzhong.constants.HttpRequestConst;
	import com.liangzhong.errortip.ErrorTips;
	import com.liangzhong.global.CMD_Head;
	import com.liangzhong.model.AppData;
	import com.liangzhong.model.youngTrain.YoungTrainServerDataParse;
	import com.liangzhong.proxy.BaseProxy;
	import com.liangzhong.proxy.ProxyUtils;
	import com.liangzhong.proxyconstant.youngTrain.YoungTrainConst;
	import com.socket.clientsocket.IClientSocket;
	
	import flash.events.Event;
	import flash.net.URLVariables;
	import flash.utils.ByteArray;
	
	import lz.controls.Alert;

	/**
	 *青训营 
	 * @author lake
	 * 
	 */	
	public class YoungTrainProxy extends BaseProxy
	{
		public static const ProxyName :String = "YoungTrainProxy";
		
		public function YoungTrainProxy(proxyName:String=ProxyName, data:Object=null)
		{
			super(proxyName, data);
		}
		/**
		 * 数据读取
		 * @param mainCmdID
		 * @param subCmdID
		 * @param buffer
		 * @param dataSize
		 * @param clientSocket
		 * @return 
		 */		
		override public function onSocketRead(mainCmdID:uint,
											  subCmdID:uint,
											  buffer:ByteArray, 
											  dataSize:int, 
											  clientSocket:IClientSocket):Boolean
		{
			switch(mainCmdID)
			{
				case CMD_Logic.MAIN:
				{
					return onDeal(subCmdID,buffer,dataSize,clientSocket);
				}
			}
			return false;
		}
		/**
		 *处理信息 
		 * @param subCmdID
		 * @param buffer
		 * @param dataSize
		 * @param clientSocket
		 * @return 
		 * 
		 */		
		private function onDeal(subCmdID:uint, 
								buffer:ByteArray, 
								dataSize:int, 
								clientSocket:IClientSocket):Boolean
		{
			var resObj :Object;
			var retcode :int;
			switch(subCmdID)
			{
				case CMD_YoungTrain.SUB_GRADE_UP://升级
				{
					resObj = YoungTrainServerDataParse.readGradeUp(buffer);
					this.sendNotification(YoungTrainConst.GRADE_UP_RESULT,resObj);
					return true;
				}
				case CMD_YoungTrain.SUB_ADD_PLAYER_CHILD://添加球童 
				{
					resObj = YoungTrainServerDataParse.readAddPlayer(buffer);
					return true;
				}
				case CMD_YoungTrain.SUB_GET_PLAYER_CHILD://签约球童 
				{
					resObj = YoungTrainServerDataParse.readGetPlayer(buffer);
					this.sendNotification(YoungTrainConst.GET_PLAYER_RESULT,resObj);
					return true;
				}
				case CMD_YoungTrain.SUB_SALE_PLAYER_CHILD://出售球童
				{
					resObj = YoungTrainServerDataParse.readSalePlayer(buffer);
					this.sendNotification(YoungTrainConst.SALE_PLAYER_RESULT,resObj);
					return true;
				}
				case CMD_YoungTrain.SUB_GROW_QUICKLY://加速成长
				{
					resObj = YoungTrainServerDataParse.readGrowAcc(buffer);
					return true;
				}
				case CMD_YoungTrain.SUB_ADD_CHILD_GROW_NUM://增加球童生产次数
				{
					resObj = YoungTrainServerDataParse.readAddGrowNum(buffer);
					return true;
				}
			}
			return false;
		}
		/**
		 * 升级
		 */		
		public function gradeUp():void
		{
			var sendBuf :ByteArray = 
				CMD_Head.getHeadByteArrayBuff(CMD_Head.Size,CMD_Logic.MAIN,CMD_YoungTrain.SUB_GRADE_UP,0);
			sendBuf.position = 0;
			clientSocket.sendData(sendBuf,sendBuf.bytesAvailable);
		}
		/**
		 *添加球童 
		 */		
		public function addPlayer():void
		{
			var sendBuf :ByteArray = 
				CMD_Head.getHeadByteArrayBuff(CMD_Head.Size,CMD_Logic.MAIN,CMD_YoungTrain.SUB_ADD_PLAYER_CHILD,0);
			sendBuf.position = 0;
			clientSocket.sendData(sendBuf,sendBuf.bytesAvailable);
		}
		/**
		 * 签约球童 
		 * @param types
		 * 
		 */				
		public function getPlayers(types :Array):void
		{
			var len :int = types.length ;
			var sendBuf :ByteArray = 
				CMD_Head.getHeadByteArrayBuff(CMD_Head.Size + 2 + len,CMD_Logic.MAIN,CMD_YoungTrain.SUB_GET_PLAYER_CHILD,0);
			sendBuf.writeShort(len);
			var type :int;
			AppData.getInstance().youngTrainManager.saleColors = [];
			for(var i:int = 0;i<len;i++)
			{
				type = types[i];
				AppData.getInstance().youngTrainManager.saleOrgetChildIndexs.push(type);
				AppData.getInstance().youngTrainManager.saleColors.push(AppData.getInstance().youngTrainManager.getChildColorByIndex(type));
				sendBuf.writeByte(type);
			}
			sendBuf.position = 0;
			clientSocket.sendData(sendBuf,sendBuf.bytesAvailable);
			//test
			//this.sendNotification(YoungTrainConst.GET_PLAYER_RESULT,{retcode:0,stars:[20]});
		}
		/**
		 *出售球童 
		 * @param index  位置
		 * 
		 */		
		public function salePlayer(index :int):void
		{
			var sendBuf :ByteArray = 
				CMD_Head.getHeadByteArrayBuff(CMD_Head.Size + 1,CMD_Logic.MAIN,CMD_YoungTrain.SUB_SALE_PLAYER_CHILD,0);
			sendBuf.writeByte(index);
			sendBuf.position = 0;
			clientSocket.sendData(sendBuf,sendBuf.bytesAvailable);
			AppData.getInstance().youngTrainManager.saleOrgetChildIndexs.push(index);
			//test
			//this.sendNotification(YoungTrainConst.SALE_PLAYER_RESULT,{retcode:0});
		}
		/**
		 *加速成长 
		 * @param index  位置
		 * 
		 */		
		public function growAcc(index :int):void
		{
			var sendBuf :ByteArray = 
				CMD_Head.getHeadByteArrayBuff(CMD_Head.Size + 1,CMD_Logic.MAIN,CMD_YoungTrain.SUB_GROW_QUICKLY,0);
			sendBuf.writeByte(index);
			sendBuf.position = 0;
			clientSocket.sendData(sendBuf,sendBuf.bytesAvailable);
		}
		/**
		 *增加球童生产次数 
		 */		
		public function addPlayerGrowNum():void
		{
			var sendBuf :ByteArray = 
				CMD_Head.getHeadByteArrayBuff(CMD_Head.Size,CMD_Logic.MAIN,CMD_YoungTrain.SUB_ADD_CHILD_GROW_NUM,0);
			sendBuf.position = 0;
			clientSocket.sendData(sendBuf,sendBuf.bytesAvailable);
		}
		/**
		 *请求青训营信息 
		 * @param $obj
		 * 
		 */
		public function getYoungTrainInfo(obj:Object):void
		{
			var variables:URLVariables = ProxyUtils.instance.getUrlVariables();
			variables.method = HttpRequestConst.YOUNGTRAIN_GET_INFO;
			ProxyUtils.instance.webClient(getYoungTrainInfoRes,variables,false);
		}
		/**
		 *返回信息 
		 * @param $data
		 * 
		 */
		private function getYoungTrainInfoRes(data:Object):void
		{
			var str:String = String(data);
			var results:Object = JSON.decode(str);
			var obj:Object = results[0];
			if(obj.retcode == 0)
			{
				AppData.getInstance().youngTrainManager.parseBaseInfo(obj.cantera);
			}else
			{
				Alert.showErrorTip(ErrorTips.instance.getErrorTip(obj.retcode));
			}
		}
	}
}